package com.lzw.breezeNovel.webApp.service.impl.novel;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.model.entity.pojo.Category;
import com.lzw.breezeNovel.webApp.mapper.novel.CategoryMapper;
import com.lzw.breezeNovel.webApp.service.novel.CategoryService;
import com.lzw.breezeNovel.webApp.vo.common.CategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.novel.impl
 * @date 2025/3/2
 * @description 分类的服务实现类
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class CategoryServiceImpl implements CategoryService {
    /**
     * 注入mapper
     */
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据分类类型获取分类
     * @param type
     * @return
     */
    @Override
    public List<CategoryVo> getCategoryByType(Integer type) {
        //根据分类类型获取分类
        List<Category> categorys = categoryMapper.getCategoryByType(type);
        //循环遍历
        List<CategoryVo> categoryVos = categorys.stream().map(category -> {
            //创建CategoryVo对象
            CategoryVo categoryVo = new CategoryVo();
            try{
                BeanUtils.copyProperties(category,categoryVo);
                return categoryVo;
            }catch (Exception e)
            {
                log.error("拷贝失败",e);
                throw new BaseException(500,"拷贝对象时出现异常");
            }
        }).filter(categoryVo -> categoryVo != null).collect(toList());
        return categoryVos;
    }
}
